#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>


#define BUF_SIZE 30
int main(int argc,char * argvs[])
{
    int serv_sock;
    char message[BUF_SIZE];
    struct sockaddr_in serv_adr,clnt_adr;
    
    serv_sock = socket(PF_INET,SOCK_DGRAM,0);
    memset(&serv_adr,0,sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_adr.sin_port = htons(atoi(argvs[1]));

    //别忘记第二个 强转
    bind(serv_sock,(struct sockaddr*)&serv_adr,sizeof(serv_adr));
    int str_len;
    socklen_t clnt_adr_size;
    fputs("Ready to receive：\n",stdout);
    while (1)
    {
        //这里由于recvfrom最后一参数传入的是地址，所以要定义此变量
        clnt_adr_size = sizeof(clnt_adr);
        /* code */
        str_len = recvfrom(serv_sock,message,sizeof(message),0,(struct sockaddr*)&clnt_adr,&clnt_adr_size);
        printf("Receve from client: %s\n",message);
        sendto(serv_sock,message,str_len,0,(struct sockaddr*)&clnt_adr,clnt_adr_size);
    }
    close(serv_sock);
    return 0;
}